package tp.pr3.instructions;

import tp.pr3.NavigationModule;
import tp.pr3.RobotEngine;
import tp.pr3.instructions.exceptions.InstructionExecutionException;
import tp.pr3.instructions.exceptions.WrongInstructionFormatException;
import tp.pr3.items.ItemContainer;
import tp.pr3.util.InstructionParser;
import tp.pr3.util.Messages;

public class MoveInstruction implements Instruction{
	@Override
	public Instruction parse(String cad) throws WrongInstructionFormatException {
		InstructionParser.parseCommand(cad, CmdDic.moveCommand);
		return this;
	}

	@Override
	public String getHelp() {
		return CmdDic.moveHelp;
	}

	@Override
	public void configureContext(RobotEngine engine, NavigationModule navigation, ItemContainer robotContainer) {
		this.navigationModule = navigation;
		this.robotEngine = engine;
	}

	@Override
	public void execute() throws InstructionExecutionException {
		try {
			this.navigationModule.move(); 
		} catch (InstructionExecutionException ex) {
			if (ex.getMessage() == "NoStreet") 
				throw new InstructionExecutionException(Messages.OUTPUT_STREET_NO + this.navigationModule.getCurrentHeading().toString());
			throw new InstructionExecutionException(Messages.OUTPUT_STREET_CLOSED);	
		} 
		
		this.robotEngine.robotSays(this.navigationModule.getCurrentHeading().moveDirection());
		System.out.println(this.navigationModule.getCurrentPlace().toString());
		this.robotEngine.addFuel(FUEL_DECREMENT_MOVE);
		
		System.out.println(""); //puesto así por exigencias del validador
		
		this.robotEngine.printRobotState();
	}

	

	private static final int FUEL_DECREMENT_MOVE = -5;
	
	private NavigationModule navigationModule;
	private RobotEngine robotEngine;
	
}
